package com.jgp.form.controller;

import com.jgp.common.utils.JGPUtil;
import com.jgp.form.model.VirtualForm;
import com.jgp.form.props.FormPropBean;
import com.jgp.form.service.FormService;
import com.jgp.mico.client.MicroClient;
import com.jgp.mico.client.UriVariableStyle;
import com.jgp.sys.controller.JGPBackController;
import com.jgp.sys.ui.Result;
import com.jgp.sys.utils.SpringContextUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.LinkedHashMap;
import java.util.Objects;

/**
 * 项目   jgp-cloud-parent
 * 作者   loufei
 * 时间   2019-09-02
 */
@CrossOrigin
@Controller
@RequestMapping("form/formController")
public class FormController extends JGPBackController {
    
    @Autowired
    private FormPropBean propBean;
    
    @Autowired
    private FormService formService;
    
    @RequestMapping("showFormMgr")
    public String showFormMgr(){
        return page("showFormMgr");
    }
    
    @RequestMapping("addForm")
    public String addForm(){
        return page("editForm");
    }
    
    @RequestMapping("editForm/{formId}")
    public String editForm(@PathVariable String formId, Model model){
        re(model,"formId",formId);
        return page("editForm");
    }
    
    @RequestMapping("addFormAttr/{formId}")
    public String addFormAttr(@PathVariable String formId, Model model){
        re(model,"formId",formId);
        return page("editFormAttr");
    }
    
    @RequestMapping("editFormAttr/{attrId}")
    public String editFormAttr(@PathVariable String attrId, Model model){
        re(model,"attrId",attrId);
        return page("editFormAttr");
    }
    
    @RequestMapping("review/{formId}")
    public String review(@PathVariable String formId, Model model){
        re(model,"formId",formId);
        return page("review");
    }
    
    
    @RequestMapping("configFormAttr/{formId}")
    public String configFormAttr(@PathVariable String formId, Model model){
        re(model,"formId",formId);
        return page("configFormAttr");
    }
    
    @RequestMapping("showFormValueMgr")
    public String showFormValueMgr(){
        return page("showFormValueMgr");
    }
    
    @RequestMapping("selectForm")
    public String selectForm(){
        return page("selectForm");
    }
    
    @RequestMapping("addFormValue")
    public String addFormValue(String formId,String formCode,Model model){
        String bizId = JGPUtil.uuid();
        re(model,"formId",formId);
        re(model,"formCode",formCode);
        re(model, "bizId", bizId);
        setRelationPage( formId, bizId, model);
        return page("editFormValue");
    }
    
    @RequestMapping("editFormValue/{formId}/{bizId}")
    public String editFormValue(@PathVariable String formId,@PathVariable String bizId,Model model){
        re(model,"formId",formId);
        re(model, "bizId", bizId);
        setRelationPage( formId, bizId, model);
        return page("editFormValue");
    }
    
    
    @RequestMapping("editFormValueWithCode/{formCode}/{bizId}")
    public String editFormValueWithCode(@PathVariable String formCode,@PathVariable String bizId,Model model){
        re(model,"formId",formCode);
        re(model, "bizId", bizId);
        setRelationPage( formCode, bizId, model);
        return page("editFormValue");
    }
    
    private void setRelationPage(String key,String bizId,Model model){
        VirtualForm form;
        if(Objects.nonNull(propBean)&&StringUtils.isNotBlank(propBean.getMicroAppName())){
            MicroClient microClient = SpringContextUtil.getBean(MicroClient.class);
            LinkedHashMap<String,Object> uriVariables = new LinkedHashMap<>();
            uriVariables.put("key",key);
            Result result = microClient.post(propBean.getMicroAppName(), "/form/formApiController/virtualForm", Result.class, UriVariableStyle.KEY_VALUE_STYLE, uriVariables);
            form = JGPUtil.toBean(JGPUtil.toJson(result.getData("formInfo")),VirtualForm.class);
        }else {
            form = formService.queryForm(key);
        }
        if(StringUtils.isNotBlank(form.getRelation())){
            re(model,"relation",form.getRelation()+"/"+form.getCode()+"/"+bizId);
        }
    }
    
    @RequestMapping("relationPage/{formCode}/{bizId}")
    public String relationPage(@PathVariable String formCode,@PathVariable String bizId,Model model){
        re(model,"formCode",formCode);
        re(model, "bizId", bizId);
        return page("relationPage");
    }
    
}
